package com.example.edu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.edu.entity.Teacher;
import com.example.edu.service.TeacherService;
import com.example.utils.QueryTeacher;
import com.example.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-06-22
 */

@RestController
@RequestMapping("/edu")
@Api(tags = "讲师管理")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @ApiOperation(value = "查询所有导师")
    @GetMapping("/teacher")
    public Result list() {


        List<Teacher> list = teacherService.list(null);
        //map集合添加一个list集合的元素
        return Result.ok().data("teachers", list);
    }

    @ApiOperation(value = "添加一位导师")
    @PostMapping("/teacher")
    public Result addTeacher(@ApiParam(value = "请输入导师的信息") @RequestBody Teacher teacher) {
        boolean flag = teacherService.save(teacher);
        if (flag == true) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @ApiOperation(value = "修改讲师信息")
    @PutMapping("/teacher/{id}")
    public Result updateTeacherById(@ApiParam(name = "id", value = "讲师id", required =
            true) @PathVariable String id, @ApiParam(name = "teacher", value = "讲师对象", required = true) @RequestBody Teacher teacher) {
        System.out.println("agdsa");
        teacher.setId(id);
        teacherService.updateById(teacher);
        return Result.ok();
    }

    @ApiOperation(value = "分页讲师列表")
    @GetMapping("/teacher/{page}/{limit}")
    public Result pageList(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Integer page, @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Integer limit) {
        Page<Teacher> pageParam = new Page<>(page, limit);
        teacherService.page(pageParam, null);
        List<Teacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return Result.ok().data("teachers", records).data("total", total);

    }

    @ApiOperation(value = "分页条件查询讲师列表")
    @PostMapping("/teacher/{page}/{limit}")
    public Result pageListSelect(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Integer page, @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Integer limit
            , @ApiParam(name = "queryTeacher", value = "查询条件", required = false) @RequestBody(required = false) QueryTeacher queryTeacher) {
        System.out.println("haj");
        System.out.println(queryTeacher);
        Page<Teacher> pageParam = new Page<>(page, limit);
        teacherService.pageQuery(pageParam, queryTeacher);
        List<Teacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return Result.ok().data("teachers", records).data("total", total);

    }

    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("teacher/{id}")
    public Result removeById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id) {

        boolean flag = teacherService.removeById(id);
        if (flag == true) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @GetMapping("teacher/{id}")
    public Result getTeacher(@PathVariable String id) {
        Teacher teacher = teacherService.getById(id);
        System.out.println(id);
        return Result.ok().data("teacher", teacher);
    }

}

